package com.davinci.freemarket.businessLayer.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.davinci.freemarket.businessLayer.controllers.CommentsController;
import com.davinci.freemarket.businessLayer.utils.StringStandarResourcesNames;
import com.davinci.freemarket.businessLayer.utils.WebParamsStandarized;
import com.davinci.freemarket.dataModelLayer.model.CommentModel;
import com.davinci.freemarket.dataModelLayer.model.UserModel;

/**
 * Servlet implementation class CommentsServlet
 */
@WebServlet("/ResponseServlet")
public class ResponseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResponseServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		//-- model que contiene las props
		CommentModel params = new CommentModel();
		
		//-- obtengo los datos del usuario en session
		HttpSession session = request.getSession(true);
		UserModel usermodel = (UserModel)session.getAttribute("currentSessionUser");
		String userID = usermodel.getUserID();
				
		try {
			params.setResponse((String)request.getParameter(WebParamsStandarized.PARAMETER_COMMENT_RESPONSE));
			params.setCommentID(Integer.parseInt((String)request.getParameter(WebParamsStandarized.PARAMETER_COMMENT_ID)));
			params.setAnswered(1);
			
			CommentsController controller = new CommentsController();
			controller.modifyComment(params);
			
			//-- para responder a la misma pagina html o redireccionar
			String destination = "/J2EE_DAVINCI_PROJECT/servlet/UserPublicationsServlet?user_id="+userID;        
			response.sendRedirect(response.encodeRedirectURL(destination));		
		}
		catch (Throwable theException) {
//			log.error("PublisherServlet - error: " + theException.getMessage());
			System.out.println(theException);
//			getServletConfig().getServletContext().getRequestDispatcher("/jsp/forms/success.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
